﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Competition.App.Web.Models.HistoryViewModels;
using Competition.App.Web.Models.NewsViewModels;
using Competition.App.Web.ResultModels.HistoryResultModels;
using Competition.App.Web.ResultModels.NewsResultModels;
using Competition.Application.Interfaces;
using Competition.Infrastructure.Config;
using Microsoft.AspNetCore.Mvc;

namespace Competition.App.Web.Controllers
{
    /// <summary>
    /// 往届回顾控制器
    /// </summary>
    public class HistoryController : Controller
    {
        private readonly Lazy<IHistoryService> _historyService;

        public HistoryController(Lazy<IHistoryService> historyService)
        {
            _historyService = historyService;
        }
        // GET: News
        public async Task<IActionResult> Index(int page)
        {
            if (page < 1)
            {
                page = 1;
            }

            var v = await _historyService.Value.FindListAsync(page, 5);

            List<HistoryResultModel> models = v.dtos.Select(dto => new HistoryResultModel()
            {
                Id = dto.Id,
                AddTime = dto.AddTime.ToString("yyyy-MM-dd"),
                Title = dto.Title,
                Description = dto.Description,
                Cover = AppConfig.OtherConfig.AdminUrl + dto.Cover
            }).ToList();

            ViewBag.List = models;
            ViewBag.TotalData = v.pageDto.TotalRecord;
            ViewBag.CurrentPage = page;  //当前页
            return View();
        }

        // GET: News/Details/5
        public async Task<IActionResult> Details(int id)
        {
            var dto = await _historyService.Value.FindAsync(id);
            HistoryViewModel model = new HistoryViewModel()
            {
                Cover = dto.Cover,
                Description = dto.Description,
                AddTime = dto.AddTime.ToString("yyyy.M.d"),
                Title = dto.Title,
                Id = dto.Id,
                Html = dto.Html
            };
            return View(model);
        }
    }
}